'use strict';

const dayjs = require("dayjs");
module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const ApiLog = app.model.define('api_log', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    method: {
      type: STRING(10),
      allowNull: false,
      comment: 'HTTP Method',
    },
    url: {
      type: STRING(255),
      allowNull: false,
      comment: 'Request URL',
    },
    status: {
      type: INTEGER,
      allowNull: false,
      comment: 'Response Status',
    },
    ip: {
      type: STRING(50),
      allowNull: false,
      comment: 'IP Address',
    },
    country: {
      type: STRING(50),
      allowNull: true,
      comment: 'Country',
    },
    response_time: {
      type: INTEGER,
      allowNull: false,
      comment: 'Response Time (ms)',
    },
    create_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    updated_time: {
      type: DATE,
      get() {
        return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
      }
    },
  },
      {
        tableName: 'api_logs',
        timestamps: true,
        updatedAt: 'updated_time',
        createdAt: 'create_time',
      }

  );

  return ApiLog;
};
